package pro.shuangxi.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author ：mengshx
 * @date ：Created in 2022/3/29 17:16
 * @description：图片生成器
 */
public class ImageUtils {
    public static byte[] getTable(List<String> list){
        int width = 500;
        int height = list.size() * 100+100;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(Color.WHITE); // 设置背景颜色
        g2.fillRect(0, 0, width, height);// 填充整张图片(其实就是设置背景颜色)
        g2.setColor(Color.black);// 设置字体颜色
        g2.setStroke(new BasicStroke(2.0f)); // 边框加粗
        g2.drawRect(1, 1, width - 2, height - 2); // 画边框就是黑边框

        Font titleFont = new Font("宋体", Font.BOLD, 20);
        g2.setFont(titleFont);
        for (int i = 0; i < list.size(); i++) {
            int y=(i+1)*100;
            int x=20;
            g2.drawString(list.get(i), x, y);
        }
        g2.dispose();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png",outputStream);
            ImageIO.write(image, "png",new File("test.png"));
            outputStream.flush();
            return outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
